package com.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.health.mapper.CheckItemMapper;
import com.health.pojo.CheckItem;
import com.health.request.CheckItemDto;
import com.health.response.PageResult;
import com.health.response.Result;
import com.health.response.constant.MessageConstant;
import com.health.service.CheckItemService;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.web.header.writers.CacheControlHeadersWriter;

import java.util.List;

@Service(interfaceClass = CheckItemService.class)
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemMapper checkItemMapper;

    /**
     * 查询所有,返回检查项集合
     *
     * @return
     */
    @Override
    public Result<List<CheckItem>> findAll() {
        //1参数校验
        //2业务代码流程
        List<CheckItem> checkItemList = checkItemMapper.findAll();
        if(checkItemList==null||checkItemList.size()==0){
            return new Result<>("查无结果");
        }
        //3封装返回结果
        Result<List<CheckItem>> result=new Result<>(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItemList);
        return result;
    }

    /**
     * 根据条件查询
     *
     * @param checkItemDto
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public Result  search(CheckItemDto checkItemDto, Integer page, Integer pageSize) {
        if(page==null){
            page=1;
        }
        if(pageSize==null){
            pageSize=10;
        }
        CheckItem checkItem=new CheckItem();
        BeanUtils.copyProperties(checkItemDto,checkItem);
       PageHelper.startPage(page,pageSize);
       Page<CheckItem> pageList = (Page<CheckItem>) checkItemMapper.search(checkItem);
       if(pageList==null ||pageList.getTotal()==0){
           return  new Result<>("查询失败");
       }
        PageResult pageResult=new PageResult();
       pageResult.setRows(pageList.getResult());
       pageResult.setTotal(pageList.getTotal());
        return new Result (pageResult);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Override
    public Result<CheckItem> findOne(Integer id) {
        CheckItem one = checkItemMapper.findOne(id);
        if(one==null){
            return  new Result<>("检查项不存在");
        }
        return new Result<>(one);
    }

    /**
     * 根据id修改
     *
     * @param id
     * @param checkItem
     * @return
     */
    @Override
    public Result<CheckItem> edit(Integer id, CheckItem checkItem) {
        CheckItem one = checkItemMapper.findOne(id);
        if(one==null){
            return  new Result<>("检查项不存在");
        }
        checkItem.setId(id);
        checkItemMapper.edit(checkItem);
        return new Result(true,"编辑成功",null);

    }

    /**
     * 新增
     *
     * @param checkItem
     * @return
     */
    @Override
    public Result<CheckItem> add(CheckItem checkItem) {
        checkItemMapper.add(checkItem);
        return new Result(true,"新增成功",checkItem);
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @Override
    public Result delete(Integer id) {
        checkItemMapper.delete(id);
        return new Result(true,"删除成功","");
    }
}
